#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define LL long long
using namespace std;

const int kMaxN = 1e4 + 10;
struct Cus {
  int l, r;
} cus[kMaxN];

bool operator<(const Cus& lhs, const Cus& rhs) {
  return lhs.r < rhs.r;
}

vector<int> tree, used;

void Add(int pos) {
  for (; pos < tree.size(); pos += (pos & (-pos))) {
    tree[pos]++;
  }
}

int GetSum(int pos) {
  int ret = 0;
  for (; pos > 0; pos -= (pos & (-pos))) {
    ret += tree[pos];
  }
  return ret;
}

int GetSum(int l, int r) {
  return GetSum(r) - GetSum(l - 1);
}

int main() {
  freopen("ad.in", "r", stdin);
  freopen("ad.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int k, n;
  vector<pair<int, int>> pos;

  cin >> k >> n;
  for (int i = 0; i < n; i++) {
    cin >> cus[i].l >> cus[i].r;
    if (cus[i].l > cus[i].r) {
      swap(cus[i].l, cus[i].r);
    }
    pos.emplace_back(cus[i].l, 1);
    pos.emplace_back(cus[i].r, 1);
  }
  sort(cus, cus + n);
  sort(pos.begin(), pos.end());

  map<int, int> mp;
  for (int i = 1; i < pos.size(); i++) {
    pos[i].second = pos[i - 1].second + min(pos[i].first - pos[i - 1].first, k);
    mp[pos[i].first] = pos[i].second;
  }

  tree.resize(pos.back().second + 1, 0);
  used = tree;

  int ans = 0;
  for (int i = 0; i < n; i++) {
    int need = k - GetSum(mp[cus[i].l], mp[cus[i].r]);
    // cout << cus[i].l << " " << cus[i].r << ": " << need << "\n";
    for (int j = mp[cus[i].r], tmp = cus[i].r; need > 0; j--, tmp--) {
      if (used[j] == 0) {
        // cout << tmp << "\n";
        used[j] = 1;
        Add(j);
        ans++;
        need--;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}